package reflect;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Scanner;

public class ReflectDemo4 {
    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        Person p = new Person();
        p.sayHello();

        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你的类名:");
        String classname = sc.nextLine();
        System.out.println("请输入你的方法名:");
        String methodName=sc.nextLine();
        //实例化
        //Class cls=Class.forName("reflect.Person");
        Class cls=Class.forName(classname);
        Object obj=cls.newInstance();//Person p=new Person();
        //获取要调用的方法
        //Method method=cls.getMethod("sayHello");
        Method method=cls.getMethod(methodName);
        method.invoke(obj);
    }
}
